package com.lwldjx.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.lwldjx.subject.application.convert.SubjectCategoryDTOConverter;
import com.lwldjx.subject.application.convert.SubjectLabelDTOConverter;

import com.lwldjx.subject.application.dto.SubjectCategoryDTO;
import com.lwldjx.subject.application.dto.SubjectLabelDTO;
import com.lwldjx.subject.common.entity.Result;
import com.lwldjx.subject.domain.entity.SubjectCategoryBO;
import com.lwldjx.subject.domain.entity.SubjectLabelBO;
import com.lwldjx.subject.domain.service.SubjectLabelDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/subject/label")
@Slf4j
public class SubjectLabelController {
    @Resource
    private SubjectLabelDomainService subjectLabelDomainService;
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectLabelDTO subjectLabelDTO){
        try{
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController.add.dto:{}",  JSON.toJSONString(subjectLabelDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(subjectLabelDTO.getLabelName()), "标签名称不能为空");
            SubjectLabelBO subjectLabelBO= SubjectLabelDTOConverter.INSTANCE.convertBoToLabel(subjectLabelDTO);
           Boolean result=  subjectLabelDomainService.add(subjectLabelBO);
            return Result.ok(result);
        }catch (Exception e){
            log.error("SubjectLabelController.add.error:{}",e.getMessage(),e);
            return Result.fail("增加失败"+e.getMessage());
        }
    }
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectLabelDTO subjectLabelDTO){
        try{
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController.update.dto:{}",  JSON.toJSONString(subjectLabelDTO));
            }
            Preconditions.checkNotNull(subjectLabelDTO.getId(),"Id不能为空");
            SubjectLabelBO subjectLabelBO= SubjectLabelDTOConverter.INSTANCE.convertBoToLabel(subjectLabelDTO);
            Boolean result=  subjectLabelDomainService.update(subjectLabelBO);
            return Result.ok(result);
        }catch (Exception e){
            log.error("SubjectLabelController.update.error:{}",e.getMessage(),e);
            return Result.fail("更新失败"+e.getMessage());
        }
    }
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectLabelDTO subjectLabelDTO){
        try{
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController.delete.dto:{}",  JSON.toJSONString(subjectLabelDTO));
            }
            Preconditions.checkNotNull(subjectLabelDTO.getId(),"Id不能为空");
            SubjectLabelBO subjectLabelBO= SubjectLabelDTOConverter.INSTANCE.convertBoToLabel(subjectLabelDTO);
            Boolean result=  subjectLabelDomainService.delete(subjectLabelBO);
            return Result.ok(result);
        }catch (Exception e){
            log.error("SubjectLabelController.delete.error:{}",e.getMessage(),e);
            return Result.fail("删除失败"+e.getMessage());
        }
    }
    @PostMapping("/deleteById")
    public Result<Boolean> deleteById(@RequestBody SubjectLabelDTO subjectLabelDTO){
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController.deleteById.dto:{}",  JSON.toJSONString(subjectLabelDTO));
            }
            Preconditions.checkNotNull(subjectLabelDTO.getId(),"ID不能为空");
            SubjectLabelBO subjectLabelBO= SubjectLabelDTOConverter.INSTANCE.convertDtoToLabelBO(subjectLabelDTO);
            Boolean result=subjectLabelDomainService.deleteById(subjectLabelBO);
            return Result.ok(result);
        }catch (Exception e){
            log.error("SubjectLabelController.deleteById.error:{}",e.getMessage(),e);
            return Result.fail("删除失败"+e.getMessage());
        }
    }
    @PostMapping("/queryLabelByCategoryId")
    public Result<List<SubjectLabelDTO>> queryLabelByCategoryId(@RequestBody SubjectLabelDTO subjectLabelDTO){
        try{
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController.queryLabelByCategoryId.dto:{}",  JSON.toJSONString(subjectLabelDTO));
            }
            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(),"分类Id不能为空");
            SubjectLabelBO subjectLabelBO= SubjectLabelDTOConverter.INSTANCE.convertBoToLabel(subjectLabelDTO);
            List<SubjectLabelBO> resultList=subjectLabelDomainService.queryLabelByCategoryId(subjectLabelBO);
            List<SubjectLabelDTO> subjectLabelDTOList=SubjectLabelDTOConverter.INSTANCE.convertBoToLabelDTOList(resultList);
            return Result.ok(subjectLabelDTOList);
        }catch (Exception e){
            log.error("SubjectLabelController.queryLabelByCategoryId.error:{}",e.getMessage(),e);
            return Result.fail("查询分类下标签失败"+e.getMessage());
        }
    }
}
